package other;

/**
 * Created by lyyn on 2022/7/20.
 */
public class ErFenSearch_704 {
    public static void main(String[] args) {
        int[] nums = new int[]{-1,0,3,5,9,12};
        Solution_0 solution = new Solution_0();
//        int target_1 = solution.search_1(nums,13);
//        int target_2 = solution.search_2(nums,13);
        int target_3 = solution.search_3(nums,13);
//        System.out.println("[left,right]:"+target_1);
//        System.out.println("[left,right):"+target_2);
        System.out.println("(left,right]:"+target_3);
    }
}
class Solution_0 {
    //[left,right]
    public int search_1(int[] nums, int target) {
        int mid = nums.length / 2;
        int left = 0;
        int right = nums.length - 1;
        while (left <= right){
            if(nums[mid] < target){
                left = mid + 1;
            }
            if(nums[mid] > target){
                right = mid - 1;
            }
            if(nums[mid] == target){
                return mid;
            }
            mid = (left + right) / 2;
        }
        return -1;
    }
    //[left,right){-1,0,3,5,9,12}_ 13
    public int search_2(int[] nums, int target) {
        int mid = nums.length / 2;
        int left = 0;
        int right = nums.length;
        while (left < right){
            if(nums[mid] < target){
                left = mid + 1;
            }
            if(nums[mid] > target){
                right = mid;
            }
            if(nums[mid] == target){
                return mid;
            }
            mid = (left + right) / 2;
        }
        return -1;
    }
    //(left,right]:_{-1,0,3,5,9,12} 13
    public int search_3(int[] nums, int target) {
        int mid = 0;
        int left = -1;
        int right = nums.length - 1;
        while (left < right){
            mid = left + (right - left + 1) / 2;
            if(nums[mid] < target){
                left = mid;
            }
            if(nums[mid] > target){
                right = mid - 1;
            }
            if(nums[mid] == target){
                return mid;
            }

        }
        return -1;
    }
}